Skip to content

Add fluent time predicates: state_changed_within?, state_updated_within?, state_changed_since?, and state_updated_since?#536

Closed
jimtng wants to merge 1 commit into
openhab:mainfrom
jimtng:item-state-changed_within
Closed

Add fluent time predicates: state_changed_within?, state_updated_within?, state_changed_since?, and state_updated_since?#536
jimtng wants to merge 1 commit into
openhab:mainfrom
jimtng:item-state-changed_within

Conversation

@jimtng

@jimtng jimtng commented May 12, 2026

Copy link
Copy Markdown
Contributor

I must admit, every time I wrote Item.last_state_change > 1.hour.ago I had to slow down and think carefully whether it's what I actually wanted.

Hopefully these fluent predicates make it easier to write / reason about.

MyItem.state_changed_within?(5.minutes) # MyItem.last_state_change >= 5.minutes.ago
MyItem.state_changed_since?(Sunset.state) 

@jimtng jimtng force-pushed the item-state-changed_within branch from 5773b11 to f104d7f Compare May 12, 2026 10:47
@jimtng jimtng changed the title Add fluent time predicates: changed_within?, updated_within?, changed_since?, and updated_since? @jimtng Add fluent time predicates: last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? May 12, 2026
@jimtng jimtng changed the title @jimtng Add fluent time predicates: last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? @jimtng Add fluent time predicates: last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? May 12, 2026
@jimtng jimtng changed the title @jimtng Add fluent time predicates: last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? Add fluent time predicates: last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? May 12, 2026
@jimtng jimtng added the enhancement New feature or request label May 12, 2026
@jimtng jimtng marked this pull request as ready for review May 12, 2026 10:53
@jimtng jimtng force-pushed the item-state-changed_within branch from f104d7f to 2bc9fb4 Compare May 12, 2026 11:12
@jimtng jimtng requested a review from Copilot May 13, 2026 03:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds fluent, intention-revealing time predicates on Items to simplify checking whether an item’s state was changed/updated “within” a duration or “since” a timestamp, reducing ambiguity compared to manual timestamp comparisons.

Changes:

  • Added last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? (plus state_* aliases) on OpenHAB::Core::Items::Item.
  • Added RSpec coverage for the new predicates, including argument validation and #to_zoned_date_time interoperability.
  • Updated DSL persistence documentation examples to use timestamp-based calls (*.ago) consistent with the persistence API.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/openhab/core/items/item.rb Introduces the new fluent time predicate APIs and aliases on Items.
spec/openhab/core/items/item_spec.rb Adds specs validating predicate behavior and accepted argument shapes.
lib/openhab/dsl.rb Adjusts persistence doc examples to pass timestamps (e.g., 1.hour.ago) and correct predicate naming.
Comments suppressed due to low confidence (2)

lib/openhab/core/items/item.rb:165

  • The documentation note for last_changed_since? says it uses #last_state_update, but the implementation compares against last_state_change. Please update the note to reference #last_state_change (and keep the persistence distinction).
        # Check if the item's state has changed since a certain time (inclusive).
        #
        # Note: This method uses the Item's {#last_state_update} timestamp, which is an internal
        # timestamp maintained within the Item object, independent of any persistence data.
        # This is different from {Persistence#changed_since?}, which is based on Persistence data.

lib/openhab/core/items/item.rb:155

  • The ArgumentError message says the argument must be a "Duration", but this method accepts any TemporalAmount that responds to #ago (including java.time.Period from 1.day). Consider updating the message (and corresponding spec) to match the actual accepted type.
        def last_updated_within?(duration)
          raise ArgumentError, "duration must be a Duration" unless duration.respond_to?(:ago)


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/openhab/core/items/item.rb Outdated
Comment thread lib/openhab/core/items/item.rb Outdated
@jimtng jimtng force-pushed the item-state-changed_within branch from 2bc9fb4 to 8fd8c5b Compare May 13, 2026 04:09
@jimtng jimtng changed the title Add fluent time predicates: last_changed_within?, last_updated_within?, last_changed_since?, and last_updated_since? Add fluent time predicates: state_changed_within?, state_updated_within?, state_changed_since?, and state_updated_since? May 13, 2026
@jimtng jimtng force-pushed the item-state-changed_within branch 2 times, most recently from 7cc90f6 to c983761 Compare May 13, 2026 04:16
@jimtng jimtng requested a review from Copilot May 13, 2026 04:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (2)

lib/openhab/core/items/item.rb:178

  • state_changed_since? will raise if last_state_change is nil (no prior change). Predicate methods should generally be safe to call; consider treating a nil timestamp as "has not changed since" and returning false.
        def state_changed_since?(time)
          last_state_change >= time
        end

lib/openhab/core/items/item.rb:192

  • state_updated_since? will raise when last_state_update is nil (no prior update). Consider returning false in that case so callers don't need to special-case initialization.
        def state_updated_since?(time)
          last_state_update >= time
        end

Comment thread lib/openhab/core/items/item.rb Outdated
Comment thread lib/openhab/core/items/item.rb Outdated
Comment thread spec/openhab/core/items/item_spec.rb
@jimtng jimtng force-pushed the item-state-changed_within branch from c983761 to 3de8468 Compare May 13, 2026 04:50
@jimtng jimtng requested a review from Copilot May 13, 2026 05:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (2)

lib/openhab/core/items/item.rb:167

  • This method unconditionally calls last_state_change, which is only present in openHAB 5.0+. Since the library still supports older versions (e.g., the respond_to?(:last_state) guard in #inspect), consider checking respond_to?(:last_state_change) first to avoid NoMethodError on openHAB 4.3.
        def state_changed_since?(time)
          changed_at = last_state_change
          return false if changed_at.nil?

          changed_at >= time

lib/openhab/core/items/item.rb:203

  • This predicate calls last_state_update without checking availability. If run against openHAB versions prior to 5.0, this will raise NoMethodError. Consider a respond_to?(:last_state_update) check (consistent with the existing OH 4.3 compatibility guard in #inspect).
        def state_updated_since?(time)
          updated_at = last_state_update
          return false if updated_at.nil?

          updated_at >= time

Comment thread lib/openhab/core/items/item.rb
Comment thread lib/openhab/core/items/item.rb
…hin?`, `last_changed_since?`, and `last_updated_since?`

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng jimtng force-pushed the item-state-changed_within branch from 3de8468 to d300161 Compare May 13, 2026 05:19
@jimtng jimtng requested a review from Copilot May 13, 2026 05:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@jimtng jimtng requested a review from ccutrer May 13, 2026 05:34
@jimtng jimtng marked this pull request as draft May 28, 2026 14:24
@jimtng

jimtng commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

I'm having second thoughts on this. Perhaps we should add something more generic against all date/time stuff instead.

e.g.

MyItem.last_state_change.within?(5.minutes)
MyItem.last_state_change.before?(SunsetTime.state)

Essentially

date_time1.within?(duration)
date_time1.before?(date_time2)
date_time1.after?(date_time2)

@jimtng

jimtng commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

superseded by #538

@jimtng jimtng closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants