How to assign current time to a sensor in an if-then template sensor

Hi,
I am putting a ultrasonic distance sensor in my letter box to see if its empty or not, if not display time sensors threshold was triggered, going in circles looking for the easiest way to display this in the in the front end. The way I was thinking was:

sensor:
  - platform: template
    sensors:
      object_in_letterbox_1:
        value_template: >-
          {% if states.sensor.letterbox_sensor.state | float <0.20 %}
          #Mail Delivered
          # Assign sensor.mail_delivered_time = current time

          ...?

          {% endif %}

I just cant get the syntax correct to assign the current system time to sensor.mail_delivered_time

Thanks
(Using latest version of hassio)

sensor:
  - platform: template
    sensors:
      object_in_letterbox_1:
        entity_id: sensor.letterbox_sensor
        value_template: >-
          {% if states('sensor.letterbox_sensor') | float <0.20 %}
            {{ now().strftime("%H:%M")}}
          {% else %}
            Undefined 
          {% endif %}

This will give the time only in HH:MM format. Other formats and the date are possible.

http://strftime.org/

You can use any string in place of Undefined, e.g. None, No Mail etc…

Paste this “starter set” into your Template Editor and experiment with them to understand how they work.

{{now()}}
{{now().date()}}
{{now().time()}}
{{now().strftime("%Y-%m-%d %H:%M")}}
{{ as_timestamp(now()) | timestamp_custom("%B %d %H:%M") }}
{{ as_timestamp(now()) | timestamp_custom("%b %d %-H:%M") }}

OK, thanks but the bit Im stuck on is how to assign this time to a different sensor (sensor.mail_delivered_time) to the one the if statement is testing on (sensor.letterbox_sensor)?

So call it that:

sensor:
  - platform: template
    sensors:
      mail_delivered_time:
        entity_id: sensor.letterbox_sensor
        value_template: >-
          {% if states('sensor.letterbox_sensor') | float <0.20 %}
            {{ now().strftime("%H:%M")}}
          {% else %}
            Undefined 
          {% endif %}

Awesome ! Working now.
Thank you, couldnt see the tree’s from the forest…

here is another (hopefully :wink:) useful thread:

In testing this more I have noticed if an item is sitting in letterbox it keeps updating the time every time the sensor updates, how would we make it only display the time of the initial trigger?

Oh yeah. That’s a problem.

Thinking out loud:

Probably better done with an automation. That way you have control over the updates.

Do you have an MQTT server set up?

The way I’d work it is:

  1. automation triggers on letterbox_sensor <0.20
  2. checks a condition to see if the ‘letterbox is full’ (input boolean) if yes --> exit
  3. if no, set the letterbox full input boolean true
  4. publish the time to an mqtt topic.

Create an MQTT sensor to read the time topic.

  1. Create another automation triggered on letterbox_sensor > x (value for no mail in box)
  2. Actions are to clear the input boolean and publish a string to the mqtt topic like ‘Mailbox Empty’.

OK, almost got it working…
Action is triggering and changing state of input boolean is changing, only the mqtt is not publishing, is anything wrong with the automation code below?
I have setup a test switch publishing to the same mqtt topic and I can verify the switch-publish with mqttlens so I know the mqtt server is working.

- id: '1555398409184'
  alias: Check Letterbox
  trigger:
  - below: '0.10'
    entity_id: sensor.letterbox_sensor
    platform: numeric_state
  condition:
  - condition: state
    entity_id: input_boolean.letterbox_full
    state: 'off'
  action:
  - service: mqtt.publish
    data_template:
      payload_template: '{{ sensor.time.payload }}'
      topic: letterbox
  - service: input_boolean.turn_on
    entity_id: input_boolean.letterbox_full

Do you actually have this sensor?

'{{ sensor.time.payload }}'

If not , try htis

action:
  - service: mqtt.publish
    data_template:
      payload: '{{ now().strftime("%H:%M") }}'
      topic: letterbox
  - service: input_boolean.turn_on
    entity_id: input_boolean.letterbox_full

That line works.
Thank you for your help, greatly appreciated.

1 Like

Hello

This is interesting.
I would like to do same to calculate automatically energy cost with sensor already provided by shelly EM.
the clamp of the sensor is installed on Line cable of electric meter. As i have also solar system, exceed solar power is resend to network.
shelly EM will sum them, and then display positive (consumption) or negative (exceed resend to network).
as price is not same for consumption and production, i want to calculate cost on an sensor, but with condition, as following.

    sensors:
      cost_shem_1_consumption_daily_kwh:
	    friendly_name: 'Cost Daily'
		value_template: >-
                  {% if states('energy_shem_1_usage_daily_kwh_normal')|float > 0 %}
                       sensors:
			  cost_shem_1_consumption_daily_kwh
			    value_template: "{{ ((states('sensor.energy_shem_1_usage_daily_kwh')|float*0.25)) + 4475) *0.09)|float) |round(0) }}"
			    unit_of_measurement: "€"
          {% else %}
		    sensors:
			  cost_shem_production_daily_kwh:
				value_template: "{{ (states('sensor.energy_shem_1_usage_daily_kwh')|float*0.12)|round(0) }}"
				unit_of_measurement: "XPF"
          {% endif %}

Does it sound correct ?
any other simple splution?
Thanks for any help.

Jacques

You can assign a template to an option:

  value_template: "{{ your template goes here }}"

but you cannot have the template contain options:

  value_template: "{{ your template goes here and it contains options}}"

Your Template Sensor attempts to re-define itself using a template that sets various options (the sensor’s name, value_template, and unit_of_measurement). This is not allowed.

thanks for your answer Taras.

I try to do differently then.
any different approach welcome.
Jacques