Crypto Light - Looking to flash Wiz bulb red when PL is negative or green when positive

Found this thread which started me on this path:

https://community.home-assistant.io/t/bitcoin-monitoring-with-home-assistant/199100

I am not using the bitcoin sensor, instead i created a statistics sensor that I want to monitor. This sensor returns € x.xx or € -x.xx (positive/negative) but i cannot seem to get the light to work. I know the script from @Krisseck (thank you sir) works as i can run it successfully.

My automation script:

- alias: PNL drop
  trigger:
  - platform: template
    value_template: '{% if (states.sensor.profitloss_status.from_state.state | float - states.sensor.profitloss_status.to_state.state | float) / states.sensor.profitloss_status.to_state.state | float < -0.05 %}true{%endif %}'
  action:
    service: script.turn_on
    entity_id: script.flash_light_red
  id: 8d30333811964e3dbb69d3ca5833aafc
- alias: PNL increase
  trigger:
  - platform: template
    value_template: '{% if (states.sensor.profitloss_status.from_state.state | float - states.sensor.sensor.profitloss_status.to_state.state | float) / states.sensor.profitloss_status.to_state.state | float > 0.05 %}true{%endif %}'
  action:
    service: script.turn_on
    entity_id: script.flash_light_green
  id: f26e732f52a7444fb998aa07d93f847c

Any input is greatly appreciated.

See if this works. It’d be fairly simple to convert them into a single automation, but let’s check it works first. Triggers off any change to the sensor, then works out whether to proceed with the flashing light.

- alias: PNL drop
  id: 8d30333811964e3dbb69d3ca5833aafc

  trigger:
  - platform: state
    entity_id: sensor.profitloss_status

  condition:
    - condition: template
      value_template: >-
        {% set pl_from = trigger.from_state.state|float %}
        {% set pl_to = trigger.to_state.state|float %}
        {{ (pl_from - pl_to) / pl_to < -0.05 }} 

  action:
    service: script.turn_on
    entity_id: script.flash_light_red

- alias: PNL increase
  id: f26e732f52a7444fb998aa07d93f847c

  trigger:
  - platform: state
    entity_id: sensor.profitloss_status

  condition:
    - condition: template
      value_template: >-
        {% set pl_from = trigger.from_state.state|float %}
        {% set pl_to = trigger.to_state.state|float %}
        {{ (pl_from - pl_to) / pl_to > 0.05 }} 

  action:
    service: script.turn_on
    entity_id: script.flash_light_green

Notes:

  • I don’t think you can use the from_state and to_state in the trigger like you were trying to, but happy to be proven wrong. Docs.
  • It’s better to use states('entity_id') than states.entity_id.state. See warning here.
  • You had an extra sensor. in your increase template.

@Troon It works!
I am always humbled at this community and its oracles, I have much to learn yet but your help will help immensely.

Thank you so much!

1 Like

Here’s a combined version (totally untested). The condition looks for an absolute value out of your formula greater than 0.05, which also includes less than -0.05 — then the choose in the action (docs) decides which colour to flash depending on whether from is larger or smaller than to. It’s possible I have the colours the wrong way around: not sure I’m following your calculation correctly.

- alias: PNL indicator
  id: b009a77c-4b7f-4469-9b92-46d8ef2de130

  trigger:
  - platform: state
    entity_id: sensor.profitloss_status

  condition:
    - condition: template
      value_template: >-
        {% set pl_from = trigger.from_state.state|float %}
        {% set pl_to = trigger.to_state.state|float %}
        {{ ((pl_from - pl_to) / pl_to)|abs > 0.05 }} 

  action:
    - choose:
        - conditions: "{{ trigger.from_state.state|float > trigger.to_state.state|float }}"
          sequence:
            - service: script.turn_on
              entity_id: script.flash_light_green
        - conditions: "{{ trigger.from_state.state|float < trigger.to_state.state|float }}"
          sequence:
            - service: script.turn_on
              entity_id: script.flash_light_red

The combined also works, i reversed the colors. Now my “Lambo” light works perfectly.
thank you

1 Like

You’re welcome. Just imagining your house lighting up like the USS Enterprise on red alert every time Elon drops a negative tweet…

Don’t tempt me… might get too much aggro from the girlfriend YOLO