Templating boolean to numeric 0/1

Hi
just struggle how to template a boolean with on/off state into a numeric
value which has only 1/0 state. Could anybody help on this, pelase? Thank you - Dave

That’s what I tried, but no success, any help would be appreciated, thank you.

  • platform: template

    sensors:

    nacht_01:
    
      friendly_name: "Nacht_01"
    
      value_template:
    
        {{ if is_state('binary_sensor.nacht', "off") }}
    
        {{ set nacht_01 = 0 %}}
    
        {% else %}
    
        {% set nacht_01 = 1 %}
    
        {% endif %}