Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

easy computed items #459

Open
ccutrer opened this issue Dec 8, 2021 · 2 comments
Open

easy computed items #459

ccutrer opened this issue Dec 8, 2021 · 2 comments

Comments

@ccutrer
Copy link
Collaborator

ccutrer commented Dec 8, 2021

I've been playing around with Home Assistant a bit just to be familiar with the broader ecosystem, and one thing I'm somewhat jealous of is how easy it is to declare a calculated entity. OpenHAB can do some simple calculations (SUM, AVG) with groups, but not more complex, arbitrary calculations. It's relatively simple to do with rules, but I'd like to be even simpler. I'm thinking:

computed_item HeatPump_Air_Differential, dependencies: [HeatPump_EnteringAir, HeatPump_LeavingAir] { HeatPump_LeavingAir - HeatPump_EnteringAir }

which would translate to

rule "#{target_item.name} Calculation" do
  changed dependencies
  run do
    unless dependencies.all?(&:state?)
      target_item.update(NULL)
      next
    end

    target_item.update(block.call)    
  end
end

It'd be even cooler if we could automatically figure out the dependencies from the block, but that's likely hard even in the simple case, and not possible if the block calls other methods.

@boc-tothefuture
Copy link
Owner

boc-tothefuture commented Dec 10, 2021

FYI - Checking is built in with only_if:

rule "#{target_item.name} Calculation" do
  changed dependencies
  run {  target_item.update(block.call) }
  only_if dependencies
  otherwise { target_item.update(NULL) }
end

@ccutrer
Copy link
Collaborator Author

ccutrer commented Dec 10, 2021

Except that that's not the same thing. Passing items directly to only_if will call truthy? on them, not check if they have a state. For several things it's equivalent, but notably a SwitchItem will confuse things. For example:

computed_item Relevant_Temperature, dependencies: [Room1_Temp, Room2_Temp, Room1Occupied_Switch] { Room1Occupied_Switch.state ? Room1_Temp : Room2_Temp }

If it were to use only_if, the final item would be NULL when it should be from room 2.

In general I dislike only_if and otherwise anyway. They only seem to serve as (confusing) syntactic sugar for core language constructs that already exist. They might save a level of nesting, but that can usually be ameliorated by using a guard clause anyway.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants