How to update this binary_sensor using now() more frequently than once a minute

Sorry to turn to the community, but I completely forgot the trick to have this binary_sensor update upon triggering of the automation the sensor is based on.
That is to say, have it update within the minute, and make the <15 mean anything… :blush: :blush:
please give me a hand…

binary_sensor:

    motion_light:
      value_template: >
        {{(now() -
             state_attr('automation.update_light_motion','last_triggered')).total_seconds()
             < 15}}

I need this because these are triggering on Ikea motion sensors that dont have a state in HA, and so the automation triggers on the lights they turn on.

frontend representation in a button:

type: custom:button-card
template: button_body
entity: variable.light_motion
variables:
  action_color: >
    [[[ return states['binary_sensor.motion_light'].state == 'on'
        ? 'gold' : 'grey'; ]]]

tap_action:
  action: more-info
hold_action:
  action: navigate
  navigation_path: >
    [[[ return (window.location.pathname.split('/')[2] == 'alarm')
        ? '/ui-settings/settings_motion' : null; ]]]
name: >
  [[[ var fakestate = `<span style="color:${variables.action_color};font-size:11px;word-wrap:break-word;">
      ${entity.state.substr(0,entity.state.length-10)}</span>`;
      return 'Light motion' + "<br>" + fakestate; ]]]
show_state: false
show_last_changed: true
state:
  - operator: default
    styles:
      card:
        - color: grey
      icon:
        - color: >
            [[[ return variables.action_color; ]]]

off:
Schermafbeelding 2021-03-09 om 15.40.39

on:
Schermafbeelding 2021-03-09 om 15.40.50

motion sensitivity needs updating to more sensitive than once per minute
again, sorry…

Could you not just add:

{% set x = entity_id of lights you react on %}

to the binary sensor value template, so it updates whenever the lights they react on changes and every minute.

Yes!, was just coming back with:

      value_template: >
        {% set updater = states('sensor.sensors_huidig_verbruik_summed') %}
        {{(now() -
             state_attr('automation.update_light_motion','last_triggered')).total_seconds()
             < 10}}

my most frequently updated sensor :wink: thanks! was buried somewhere deep…
not using the lights though, because they tend to be ‘on’ longer than the automation is needed to be responsive. It’s a real hack, but hey, whatever makes it work makes it work.

Schermafbeelding 2021-03-09 om 16.01.23

this will do ok :+1:

type: custom:button-card
template: button_body
entity: variable.light_motion
variables:
  action_color: >
    [[[ return states['binary_sensor.motion_light'].state == 'on'
        ? 'gold' : 'grey'; ]]]
tap_action:
  action: more-info
hold_action:
  action: navigate
  navigation_path: >
    [[[ return (window.location.pathname.split('/')[2] == 'alarm')
        ? '/ui-settings/settings_motion' : null; ]]]
icon: >
  [[[ return states['binary_sensor.motion_light'].state == 'on'
        ? 'mdi:lightbulb-on' : 'mdi:lightbulb-off'; ]]]
name: >
  [[[ var fakestate = `<span style="color:${variables.action_color};font-size:11px;word-wrap:break-word;">
      ${entity.state.split(':')[0]}</span>`;
      return 'Light motion' + "<br>" + fakestate; ]]]
show_state: false
show_last_changed: true
styles:
  card:
    - color: grey
  icon:
    - color: >
        [[[ return variables.action_color; ]]]
1 Like

I actually have a sensor that does a one second update:

sensor:
  - platform: template
    sensors:
      one_second_update:
        value_template: "{{now().strftime('%-S')}}"

automation:
  - alias: One Second Updater
    trigger:
      platform: time_pattern
      seconds: '/1'
    action:
      service: homeassistant.update_entity
      entity_id: sensor.one_second_update
2 Likes

Ha, yes but that’s unfair, and uses an extra automation :wink:

Have the same autom for testing purposes to show a second in the frontend . I’ve disabled it though for every second seems not necessary and an over-burden to the system.

My energy sensor is updated by the systems internal processes already available, and in its slowest updates takes 6 seconds…

That’s probably a stretch.

Having something update every second is a piece of cake for a microprocessor that runs in the very low millisecond cycle time range.

But yeah if you don’t need it… :slightly_smiling_face:

getting back to this, upon reloading templates, this is always displayed in the logs:

TemplateError('UndefinedError: 'sensor' is undefined') while processing template 'Template("{% set updater = sensor.sensors_huidig_verbruik_summed %} {{(now() - state_attr('automation.update_light_motion','last_triggered')).total_seconds() < 10}}")' for attribute '_state' in entity 'binary_sensor.motion_light'

which is odd, since the motion_light binary doesnt have an attribute ‘_state’

I tried changing to

{% set updater = sensor.sensors_huidig_verbruik_summed %}

since the template there was maybe an error, but the result remains identical

now this is not really a big issue, but Id like to get rid of the error nonetheless. would this need the availability_template ? if so, what would be the best to use?

Genious! Thanks

1 Like